package com.cxf.windex.biz;

import com.cxf.windex.base.bean.LockInfo;
import com.cxf.windex.base.biz.BaseBiz;
import com.cxf.windex.base.common.ServiceException;
import com.cxf.windex.base.constant.LockConstant;
import com.cxf.windex.base.constant.UserConstant;
import com.cxf.windex.entity.BOrders;
import com.cxf.windex.entity.BShop;
import com.cxf.windex.entity.Money;
import com.cxf.windex.enums.OperateEnum;
import com.cxf.windex.mapper.MoneyMapper;
import com.cxf.windex.task.LogTask;
import java.math.BigDecimal;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

/**
 * @program: business_needs
 * @description: 钱包Biz
 * @author: chenxf
 * @create: 2018-10-26 16:03
 **/
@Slf4j
@Service
public class MoneyBiz extends BaseBiz<MoneyMapper,Money> {

  @Autowired
  private BShopBiz bShopBiz;
  @Lazy
  @Autowired
  private LogTask logTask;
  @Autowired
  private BOrdersBiz ordersBiz;
  @Autowired
  private JsUserBiz jsUserBiz;

  /**
   * 冻结广告费
   * @param wxopenid 微信openId
   * @param adMoney  广告费
   * @param type 支付类型 0余额支持
   * @param orderId 订单ID
   * @param password 支付密码
   */
  public void orderPay(String  wxopenid,String adMoney,int type,String orderId,String password) {
    BOrders orders=ordersBiz.selectById(orderId);
    if(orders==null){
      throw new ServiceException("订单异常");
    }
    if("1".equals(orders.getStatus())){
      throw new ServiceException("订单已支付");
    }
    Money money;
    switch (type) {
      case 0:
        //余额支付
       // jsUserBiz.checkPassword(wxopenid,password);
         money=getInfo(wxopenid);
        if(money.getCost().compareTo(new BigDecimal(adMoney)) < 0){
          throw new ServiceException("余额不足");
        }
        money.setCost(money.getCost().subtract(new BigDecimal(adMoney)));
        logTask.doQbzz(money,OperateEnum.COST_ORDERS,UserConstant.SHOP_ORDERS+adMoney+"元",adMoney,UserConstant.SHOP_RANGE,orderId);
        break;
      case 1:
        //佣金支付
       // jsUserBiz.checkPassword(wxopenid,password);
        money=getInfo(wxopenid);
        if(money.getIncome().compareTo(new BigDecimal(adMoney)) < 0){
          throw new ServiceException("余额不足");
        }
        money.setIncome(money.getIncome().subtract(new BigDecimal(adMoney)));
        logTask.doQbzz(money,OperateEnum.INCOME_ORDERS,UserConstant.SHOP_ORDERS+adMoney+"元",adMoney,UserConstant.SHOP_RANGE,orderId);
        break;
      case 2:
        //微信支付
         money=getInfo(wxopenid);
        logTask.doQbzz(money,OperateEnum.WX_ORDERS,UserConstant.SHOP_ORDERS+adMoney+"元",adMoney,UserConstant.SHOP_RANGE,orderId);
        break;
      default:
        throw new ServiceException("暂不支持");
    }

  }

  /**
   * 认证
   * @param wxopenid 微信openId
   * @param certFee  认证费
   * @param  range 0店铺 1资源
   */
  public void cert(String  wxopenid,String certFee,String range) {
    switch (range){
      case UserConstant.SHOP_RANGE:
        BShop bShop=bShopBiz.getInfo(wxopenid);
        if("0".equals(bShop.getStatus())){
          bShop.setStatus("1");
          bShopBiz.updateSelectiveById(bShop);
          //生成认证费记录
          logTask.accountLog(getInfo(wxopenid).getName(),wxopenid,OperateEnum.WX_CERT,UserConstant.SHOP_CERT,certFee,UserConstant.SHOP_RANGE,null);
        }else {
          throw new ServiceException("用户已认证");
        }
        break;
      case UserConstant.RESOURCE_RANGE:
        break;
      default:
        break;
    }
  }

  /**
   * 充值
   * @param wxopenid 微信openId
   * @param rechargeMoney  充值金额
   * @param  range 0店铺 1资源
   */
  public void recharge(String  wxopenid,String rechargeMoney,String range) {
    switch (range){
      case UserConstant.SHOP_RANGE:
        //生成充值记录
        Money money=getInfo(wxopenid);
        money.setCost(money.getCost().add(new BigDecimal(rechargeMoney)));
        logTask.doQbzz(money,OperateEnum.COST_RECHARGE,UserConstant.SHOP_RECHARGE,rechargeMoney,range,null);
        break;
      case UserConstant.RESOURCE_RANGE:
        break;
      default:
        break;
    }
  }

  /**
   * 提现申请
   *
   * @param wxopenid  用户ID
   * @param zcCount 提现金额
   * @param notes   备注
   */
  public void cash(String wxopenid, String zcCount, String notes,String range) {
    if (StringUtils.isBlank(zcCount)) {
      throw new ServiceException("提现金额不能为空");
    }
    if (StringUtils.isBlank(notes)) {
      throw new ServiceException("提现账号不能为空");
    }

    //账号转入
    LockInfo lockInfo = new LockInfo(LockConstant.USER_LOCK + wxopenid);
    try {
      Money money = this.getInfo(wxopenid);
      if (money == null) {
        lockInfo.unlock();
        throw new ServiceException("用户资金信息查询失败");
      }


      if (money.getIncome().compareTo(new BigDecimal(zcCount)) < 0) {
        lockInfo.unlock();
        throw new ServiceException("佣金钱包余额 不足");
      }
      money.setIncome(money.getIncome().subtract(new BigDecimal(zcCount)));
      money.setWithdraw(money.getWithdraw().add(new BigDecimal(zcCount)));
      logTask.doQbzz(money, OperateEnum.INCOME_CASH, notes, zcCount,range,null);

    } catch (Throwable throwable) {
      lockInfo.unlock();
      throw new ServiceException(throwable.getMessage());
    } finally {
      lockInfo.unlock();
    }
  }

  /**
   * 获取资金信息
   * @param wxopenid 用户wxopenid
   * @return
   */
  public Money getInfo(String  wxopenid) {
    Money entity=new Money();
    entity.setWxopenid(wxopenid);
    return selectOne(entity);
  }

  /**
   * 初始化资金信息
   * @param wxopenid 用户wxopenid
   * @param  name 用户昵称
   */
  public void initMoney(String wxopenid,String name){
    Money entity=new Money();
    entity.setName(name);
    entity.setWxopenid(wxopenid);
    insertSelective(entity);
  }

}
